import { onMounted, reactive, toRefs, watch } from "vue";
import homeApi from "@/apis/home";
import { Toast } from "vant";
export default function () {
  const state = reactive({
    Banners: [],
    list: [],
    limit: 6,
    page: 1,
    total: 0,
    loading: false,
    finished: false,
  });
  const rest = (type: number) => {
    state.list = [];
    state.total = 0;
    state.page = 1;
    getList(type);
  };

  const getList = async (type: number = 0) => {
    const data = {
      limit: state.limit,
      page: state.page,
    };
    state.loading = true;
    let res;

    const toast1 = Toast.loading({
      message: "正在加载中",
      duration: 0,
    });

    if (type === 0) {
      res = await homeApi.getList(data);
    } else {
      res = await homeApi.getSoon(data);
    }
    const { object_list, total } = res;
    if (object_list.length < data.limit) {
      state.finished = true;
    }
    state.list = state.list.concat(object_list);

    state.total = total;
    state.loading = false;
    state.page++;
    toast1.clear();
  };

  onMounted(() => {
    getList();
  });

  return { ...toRefs(state), getList, rest };
}
